package com.smartscreen.baseinfomanage.service.impl;

import com.smartscreen.baseinfomanage.dto.CreateSubjectRequest;
import com.smartscreen.baseinfomanage.entity.Subject;
import com.smartscreen.baseinfomanage.mapper.SubjectMapper;
import com.smartscreen.baseinfomanage.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public List<Subject> getAllSubjects() {
        return subjectMapper.selectList(null);
    }

    @Override
    public Subject createSubject(CreateSubjectRequest request) {
        String subjectId = "sub" + UUID.randomUUID().toString().substring(0, 6);
        
        Subject newSubject = new Subject();
        newSubject.setSubjectId(subjectId);
        newSubject.setName(request.getName());
        
        subjectMapper.insert(newSubject);
        return newSubject;
    }

    public Map<String, Map<String, Object>> getSubjectsBatchAsMap(List<String> subjectIds) {
        if (subjectIds == null || subjectIds.isEmpty()) {
            return new HashMap<>();
        }

        List<Subject> subjects = subjectMapper.selectBatchIds(subjectIds);
        Map<String, Map<String, Object>> result = new HashMap<>();

        for (Subject subject : subjects) {
            Map<String, Object> subjectMap = new HashMap<>();
            subjectMap.put("subjectId", subject.getSubjectId());
            subjectMap.put("name", subject.getName());
            subjectMap.put("createdAt", subject.getCreatedAt());
            subjectMap.put("updatedAt", subject.getUpdatedAt());

            result.put(subject.getSubjectId(), subjectMap);
        }

        return result;
    }
}